package com.xiaohao.server.parkmap.controller;

import com.xiaohao.server.parkmap.entity.Article;
import com.xiaohao.server.parkmap.entity.ResponseResult;
import com.xiaohao.server.parkmap.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("{id}")
    public ResponseResult<Article> query(@PathVariable Integer id) {
        return ResponseResult.success(articleService.queryById(id));
    }

    @GetMapping("/type/{type}")
    public ResponseResult<List<Article>> queryByType(@PathVariable Integer type) {
        return ResponseResult.success(articleService.query(new Article().setType(type)));
    }

    @GetMapping
    public ResponseResult<List<Article>> query(Article article) {
        return ResponseResult.success(articleService.query(article));
    }

    @DeleteMapping("{id}")
    public ResponseResult<Void> delete(@PathVariable Integer id) {
        return articleService.delete(id) > 0 ? ResponseResult.success() : ResponseResult.error();
    }

    @PostMapping
    public ResponseResult<Void> save(@RequestBody Article article) {
        return articleService.save(article) > 0 ? ResponseResult.success() : ResponseResult.error();
    }
}
